package com.it135.citytraffic.controller;

import com.it135.citytraffic.pojo.Ming;
import com.it135.citytraffic.pojo.Xing;
import com.it135.citytraffic.pojo.entity.Survey;
import com.it135.citytraffic.pojo.result.Result;
import com.it135.citytraffic.pojo.vo.SurveyVO;
import com.it135.citytraffic.service.ISurveyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 调查问卷
 */
@Slf4j
@RestController
@RequestMapping("/api/survey")
@RequiredArgsConstructor
public class SurveyController {

    private final ISurveyService surveyService;
    private final OpenAiChatModel openAiChatModel;

    /**
     * 新增调查问卷填写信息
     *
     * @param surveys
     * @return
     */
    @CrossOrigin
    @PostMapping
    public Result save(@RequestBody List<Survey> surveys) {
        surveyService.save(surveys);
        return Result.success();
    }

    /**
     * 统计每个问题选项次数
     * @return
     */
    @CrossOrigin
    @GetMapping
    public Result<String> queryCount() {
        String result = surveyService.queryCount();
        return Result.success(result);
    }

    /**
     * 词云
     * @return
     */
    @CrossOrigin
    @GetMapping("/wordCloud")
    public Result<SurveyVO> queryWordCloud() {
        SurveyVO surveyVO = surveyService.queryWordCloud();
        return Result.success(surveyVO);
    }

    @CrossOrigin
    @PostMapping("/add")
    public void add() {
        String[] str = {"您对城市交通管理还有哪些建议？", "您认为如何才能更好地缓解交通拥堵问题？"};
        Random random = new Random();

        List<Survey> list = new ArrayList<>();
        for (int i = 0; i < random.nextInt(80,130); i++) {
            String question = str[random.nextInt(str.length)];
            String answer = openAiChatModel.call(question);
            list.add(Survey.builder()
                    .name(Xing.getXing() + Ming.getMing())
                    .question(question)
                    .answer(answer)
                    .createTime(LocalDateTime.now())
                    .build());
        }
        surveyService.saveBatch(list);
    }
}
